capture log close
log using $log/AED_dataprep_12_outcomes_prep, text replace

clear
set mem 2000m
set maxvar 10000
set matsize 800
set more off

set trace off
   
/* ----------------------------------------- */
/* --- Reduce data to one row per person --- */
/* ----------------------------------------- */
	
		



foreach iebversion in v901 {
	
		forvalue year = 2005/2008 {

			
			
			use $data/entries_outcome_`year'_`iebversion', clear
			
			
			gen olf = 0
			replace olf = 1 if ue==0 & sv==0 
			sort iza_id lfd
			foreach state in ue_ sv_ {
				by iza_id: replace `state'=1 if olf[_n-1]==0 & olf[_n+1]==0 & olf==1 & `state'[_n-1]==1 & _n>1
				by iza_id: replace `state'=1 if olf[_n-1]==0 & olf==1 & `state'[_n-1]==1 & _n==48
			}
			foreach state in geldsv_ {
				by iza_id: replace `state'=`state'[_n-1] if olf[_n-1]==0 & olf[_n+1]==0 & olf==1 & `state'[_n-1]~=0 & _n>1
				by iza_id: replace `state'=`state'[_n-1] if olf[_n-1]==0 & olf==1 & `state'[_n-1]~=0 & _n==48
			}
			drop olf
			
			
			*Keep only entries into UE Benefit 1 system
            sort iza_id lfd
			gen alg_temp = 1 if iza_leh_==1 & t==1
			by iza_id: egen alg1_eintritt=max(alg_temp)
			keep if alg1_eintritt==1
			
			*Labor Market Status for next 24 months
			forvalues t=1/24 {
			
			gen sv_tplus_temp_`t'=1 if sv_==1 & t==`t'
			by iza_id: egen sv_tplus_`t'=max(sv_tplus_temp_`t')
			replace sv_tplus_`t'=0 if sv_tplus_`t'==.
			drop sv_tplus_temp_`t'
			
			gen ue_tplus_temp_`t'=1 if ue_==1 & t==`t'
			by iza_id: egen ue_tplus_`t'=max(ue_tplus_temp_`t')
			replace ue_tplus_`t'=0 if ue_tplus_`t'==.
			drop ue_tplus_temp_`t'
			
			gen geldsv_tplus_temp_`t'=geldsv_ if t==`t'
			by iza_id: egen geldsv_tplus_`t'=max(geldsv_tplus_temp_`t')
			replace geldsv_tplus_`t'=0 if geldsv_tplus_`t'==.
			drop geldsv_tplus_temp_`t'
			
			gen geldleh_tplus_temp_`t'=geldleh_ if t==`t'
			by iza_id: egen geldleh_tplus_`t'=max(geldleh_tplus_temp_`t')
			replace geldleh_tplus_`t'=0 if geldleh_tplus_`t'==.
			drop geldleh_tplus_temp_`t'
			}/*End: t*/
			
			/*=== Right censoring ===*/
			gen zens = 1 if sv_[_n-1]==1 
			replace zens = 1 if ue_[_n-1] == 1 & ue_==0 & sv_==0
			gen olf = 1 if ue_==0 & sv_==0 /*kommt nur noch vor fuer mehr als 2 Monate (s.o.)*/
			replace zens = 1 if olf==1
			drop olf
			by iza_id: replace zens=1 if sv_[_n-1]==0 & sv_[_n-2]==1
		
			sort iza_id lfd
			by iza_id: replace zens = 1 if zens[_n-1]==1
			
			*unemployment duration
			gen durex_temp=t if sv_==1 & zens ~=1
			by iza_id: egen durex=min(durex_temp)
			
			drop durex_temp
			gen exitsv=0
			replace exitsv=1 if durex~=.
			
			*time to treatment
			foreach treat in almp ltt stt ws workfare {
			gen dur`treat'_temp=t if iza_`treat'_==1 & zens ~=1
			by iza_id: egen dur`treat'=min(dur`treat'_temp)
			drop dur`treat'_temp
			gen exit`treat'=0
			replace exit`treat'=1 if dur`treat'~=.
			}/*end: treat*/
			
			gen durzens_temp=t if zens==1
			by iza_id: egen durzens=min(durzens_temp)
			drop durzens_temp
			
				
			*Working location for following employment
			destring iza_ao_aa_,replace
			
			
			foreach state in aa  {
			gen ao_`state'_trans1=iza_ao_`state'_ if t==durex
			gen ao_`state'_trans2=iza_ao_`state'_ if t==durex+1
			by iza_id: egen ao_`state'_trans=min(ao_`state'_trans1)
			by iza_id: egen ao_`state'_trans_help=min(ao_`state'_trans2)
			replace ao_`state'_trans=ao_`state'_trans_help if (ao_`state'_trans==.|(ao_`state'_trans~=. & ao_`state'_trans==iza_wo_`state'_eintr & ao_`state'_trans~=ao_`state'_trans_help))
			}
			
			
			preserve
			
			drop if (iza_wo_kreis_==.|iza_wo_kreis_==.z|iza_wo_kreis_==.n)
			*Dummy: Relocation with 24 month
			by iza_id: gen iza_umzug_=1 if iza_wo_kreis_~=iza_wo_kreis_[_n-1] & _n~=1
			by iza_id: egen iza_umzug=max(iza_umzug_)
			replace iza_umzug=0 if iza_umzug==.
			
			
			by iza_id: keep if _n==1
			
			
		
			
			keep iza_id iza_umzug 
			tempfile umzug
			save `umzug',replace
			
			restore
			
			
			*Commuting behavior
			gen iza_commuting_=1 if iza_wo_aa_~=iza_ao_aa_ & sv_==1
			*Ever Commuting in 24 Months
			by iza_id: egen iza_commuting=max(iza_commuting_)
			
			
			
			foreach var in iza_commuting  {
			replace `var'=0 if `var'==.
			}
			
			by iza_id: keep if _n==1
			
            gen iza_benlevel=iza_leh_*30
            replace iza_benlevel=0 if iza_benlevel==.

			joinby iza_id using `umzug',unmatched(master)
			tab _merge
			drop _merge
			
			foreach var in almp ltt stt ws workfare {
			gen iza_`var'=1 if dur`var'~=.
			replace iza_`var'=0 if iza_`var'==.
			}
			
			
			
			
			saveold $data/outcome_short_`year'_`iebversion', replace
			
			
			}
			}
			
			
			
	log close
			
			
